<?php

namespace app\api\controller\car;

use app\api\controller\Controller;
use think\facade\Db;
/**
 * 个人中心主页
 */
class Order extends Controller
{
    public function order_list(){
        // 当前用户信息
        $user      = $this->getUser();
        $where     = array('status'=>0);
        $orderlist = Db::name('car_order')->where($where)->select()->toArray();
        $list      = [];
        foreach ($orderlist as $v) {
            $olist                = [];
            $olist['starting']    = $v['star_address'];
            $olist['destination'] = $v['end_address'];
            $olist['price']       = $v['price'];
            $olist['payType']     = '线上支付';
            $olist['seat']        = $v['passenger'];
            $olist['companyName'] = '测试';
            $olist['companyLogo'] = '';
            $olist['isVip']       = 1;
            $olist['feedBack']    = '98';
            $olist['issueTime']   = $v['create_time'];
            $olist['distrance']   = '';
            $olist['car_id']      = $v['id'];
            $olist['tradesNum']   = 10;
            $list[]               = $olist;
        }
        return $this->renderSuccess('', [
            'userInfo'  => $user,
            'orderlist' => $list
        ]);
    }
    public function order_info()
    {
        // 当前用户信息
        $user = $this->getUser();
        $data = $this->request->post();
        $where = array('a.id' => $data['order_id']);
        $info = Db::name('car_order')->alias('a')->where($where)->find();
        $list = [];
        $list['starting']        = $info['star_address'];
        $list['destination']     = $info['end_address'];
        $list['price']           = $info['price'];
        $list['seatBus']         = $info['passenger'];
        $list['startingTime']    = $info['star_time'];
        $list['destinationTime'] = $info['end_time'];
        $list['lineInformation'] = $info['lineInformation'];
        $list['carPurpose']      = $info['purpose_id'];
        $list['total']           = $info['total_km'];
        $list['backType']        = $info['back_type'];
        $list['modelsSaddle']    = $info['car_type'];
        $list['earnest']         = $info['front_money'];
        $list['car_id']          = $info['id'];
        $list['orderNumber']     = $info['ordernum'];
        $list['code']            = $info['star_address'];
        $list['beglongitude']    = "11.11";
        $list['beglatitude']     = "11.11";
        $list['endlongitude']    = "11.11";
        $list['beglatitude']     = "11.11";
        $list['driverCost']      = "自行解决";
        $list['car_num']         = 1;
        return $this->renderSuccess('', [
            'userInfo'  => $user,
            'orderinfo' => $list
        ]);
    }
    public function my_order_list()
    {
        $user = $this->getUser();
        $data = $this->request->post();
        $where = array('take_user_id' => $user['user_id']);
        $order = Db::name('car_order')->where($where)->select();
        $list = [];
        foreach ($order as $v) {
            $olist                = [];
            $olist['starting']    = $v['star_address'];
            $olist['destination'] = $v['end_address'];
            $olist['time']        = $v['star_time'];
            $olist['company']     = '测试';
            $olist['logo']        = '';
            $olist['number']      = $v['ordernum'];
            $olist['seat']        = '';
            $olist['practical']   = $v['passenger'];
            $olist['type']        = $v['status'];
            $olist['price']       = $v['price'];
            $olist['order_id']    = $v['id'];
            $olist['car_name']    = '';
            $olist['car_image']   = '';
            $olist['car_plate']   = '';
            $olist['car_id']      = '';
            $list[]               = $olist;
        }
        return $this->renderSuccess('', [
            'userInfo'  => $user,
            'orderlist' => $list
        ]);
    }
    public function my_order_info()
    {
        $user = $this->getUser();
        $data = $this->request->post();
        $where = array('a.id' => $data['order_id']);
        $info = Db::name('car_order')->alias('a')->where($where)->find();
        $list = [];
        $list['starting']        = $info['star_address'];
        $list['destination']     = $info['end_address'];
        $list['price']           = $info['price'];
        $list['seatBus']         = $info['passenger'];
        $list['startingTime']    = $info['star_time'];
        $list['destinationTime'] = $info['end_time'];
        $list['lineInformation'] = $info['lineInformation'];
        $list['carPurpose']      = $info['purpose_id'];
        $list['total']           = $info['total_km'];
        $list['backType']        = $info['back_type'];
        $list['modelsSaddle']    = $info['car_type'];
        $list['earnest']         = $info['front_money'];
        $list['car_id']          = $info['id'];
        $list['orderNumber']     = $info['ordernum'];
        $list['code']            = $info['star_address'];
        $list['beglongitude']    = "11.11";
        $list['beglatitude']     = "11.11";
        $list['endlongitude']    = "11.11";
        $list['beglatitude']     = "11.11";
        $list['driverCost']      = "自行解决";
        $list['company']         = "测试";
        $list['logo']            = "";
        $list['car_num']         = 1;
        $cwhere = array(
            'a.order_id' => $info['id'],
        );
        $plancar = Db::name('car_order_plan')
                ->alias('a')
                ->field('a.*,b.car_number,b.car_info3,c.realname,d.type_name')
                ->join('jjjshop_car b','a.car_id = b.car_id','LEFT')
                ->join('jjjshop_driver c','b.driver_id = c.driver_id','LEFT')
                ->join('jjjshop_car_type d','b.car_type = d.type_id','LEFT')
                ->where($cwhere)
                ->order('a.create_time desc')
                ->find();
        if (!empty($plancar)) {
            $list['car_type']   = $plancar['type_name'];
            $list['car_driver'] = $plancar['realname'];
            $list['car_plate']  = $plancar['car_number'];
            $list['car_image']  = $plancar['car_info3'];
        }
        return $this->renderSuccess('', [
            'userInfo'  => $user,
            'orderinfo' => $list
        ]);
    }
    public function my_car_go()
    {
        $user = $this->getUser();
        $where = array('a.user_id' => $user['user_id']);
        $list = [];
        return $this->renderSuccess('', [
            'userInfo'  => $user,
            'list' => $list
        ]);
    }
    public function take_order()
    {
        $user = $this->getUser();
        $data = $this->request->post();
        if (empty($data['car_id'])) {
            return $this->renderError('请选择车单');
        }
        $where = array(
            'id'       => $data['car_id'],
            'ordernum' => $data['orderNumber'],
        );
        $order = Db::name('car_order')->where($where)->find();
        if (empty($order)) {
            return $this->renderError('未知车单');
        }
        if ($order['status'] == 1) {
            return $this->renderError('该车单已被接单，请选择其他车单');
        }
        $set = array(
            'take_user_id' => $user['user_id'],
            'update_time'  => date('Y-m-d H:i:s',time()),
            'status'       => 1
        );
        $res = Db::name('car_order')->where("id =".$data['car_id'])->update($set);
        if($res){
            return $this->renderSuccess('抢单成功', []);
        }else{
            return $this->renderError('抢单失败', []);
        }
    }
    public function plan_car()
    {
        $user = $this->getUser();
        $data = $this->request->post();
        if (empty($data['order_id'])) {
            return $this->renderError('请选择车单');
        }
        if (empty($data['car_id'])) {
            return $this->renderError('请选择车辆');
        }
        $where = array(
            'order_id' => $data['order_id'],
            'car_id'   => $data['car_id'],
        );
        $plan = Db::name('car_order_plan')->where($where)->find();
        if ($plan) {
            return $this->renderError('不能重复安排同一辆车');
        }
        $inset = array(
            'order_id'    => $data['order_id'],
            'car_id'      => $data['car_id'],
            'create_time' => date('Y-m-d H:i:s',time())
        );
        $res = Db::name('car_order_plan')->insertGetId($inset);
        if($res){
            return $this->renderSuccess('安排成功', []);
        }else{
            return $this->renderError('安排失败', []);
        }
    }
}